const express = require('express');    // 框架主体
const body = require('body-parser');    // 接收普通POST数据
const multer = require('multer');    // 接收文件POST数据

// 下面是express的写法
// 创建一个服务
let server = express();
server.listen(8080);

// 添加中间件
server.use(body.urlencoded({extended:false}));
let multerObj = multer({dest: './upload'});
server.use(multerObj.any());

server.post('/api', (request, response) => {
	response.setHeader('Access-Control-Allow-Origin', '*');
	response.send('OK');
	// { user: 'admin', pwd: '123456' }
	console.log(request.body);   // 普通post数据
	console.log(request.files);   // 文件post数据
	/** 
	 [ 
	    { 
	        fieldname: 'file',
			originalname: '3333333.png',
			encoding: '7bit',
			mimetype: 'image/png',
			destination: './upload',
			filename: 'b9796d0084fe8ebc0fe206b48f955675',
			path: 'upload\\b9796d0084fe8ebc0fe206b48f955675',
			size: 98483 
		},
		{ 
			fieldname: 'file',
			originalname: '前端课程大纲(2).docx',
			encoding: '7bit',
			mimetype: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
			destination: './upload',
			filename: 'c2007f4118332ad29dc4ed46e0c787ac',
			path: 'upload\\c2007f4118332ad29dc4ed46e0c787ac',
			size: 18100 
		}
	]
	*/

})

server.use(express.static('./www/'));